﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.IBLL.PUB;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.IBLL;
using Apps.Models.PUB;
using Microsoft.Practices.Unity;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Data;
using Wantn.FIS.Enum;

namespace Apps.Web.Areas.PUB.Controllers
{
    public class TRANSPORTController : BaseController
    {
        [Dependency]
        public IPUB_TRANSPORTBLL m_BLL { get; set; }
        ValidationErrors errors = new ValidationErrors();
        
        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [SupportFilter(ActionName="Index")]
        public JsonResult GetList(GridPager pager, string queryStr)
        {
            List<PUB_TRANSPORTModel> list = m_BLL.GetList(ref pager, queryStr);
            GridRows<PUB_TRANSPORTModel> grs = new GridRows<PUB_TRANSPORTModel>();
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }
   

        #region 运输商选择窗口
        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetListNotINSupplierId(GridPager pager, string queryStr, string SuppID)
        {
            List<PUB_TRANSPORTModel> list = m_BLL.GetListNotINSupplierId(ref pager, queryStr, SuppID);
            GridRows<PUB_TRANSPORTModel> grs = new GridRows<PUB_TRANSPORTModel>();
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }

        [SupportFilter(ActionName = "Create")]
        public ActionResult MaterielSel(string SuppID)
        {
            ViewBag.SuppID = SuppID;
            ViewBag.MetStatisticsType = new SelectList(EnumMvcHelper.GetEnumAllDesc<MetStatisticsType>(), "Key", "Desc");
            return View();
        }

        #endregion
      

      
        #region 创建
        [SupportFilter]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Create(PUB_TRANSPORTModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                if (isRepeated(model))
                {
                    return Json(JsonHandler.CreateMessage(0, "已存在名称为["+ model.TRANSPORT_NAME+ "]的运输商，保存失败!"));
                }

                if (m_BLL.Create(ref errors, model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "TRANSPORT_ID" + model.TRANSPORT_ID + ",TRANSPORT_NAME" + model.TRANSPORT_NAME, "成功", "创建", "PUB_TRANSPORT");
                    return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "TRANSPORT_ID" + model.TRANSPORT_ID + ",TRANSPORT_NAME" + model.TRANSPORT_NAME + "," + ErrorCol, "失败", "创建", "PUB_TRANSPORT");
                    return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
            }
        }
        #endregion
        //是否重复
        private bool isRepeated(PUB_TRANSPORTModel model)
        {
            bool result = false;
            List<PUB_TRANSPORTModel> oldList = m_BLL.GetList(p => p.TRANSPORT_NAME == model.TRANSPORT_NAME && p.TRANSPORT_ID!=model.TRANSPORT_ID);
            if (oldList.Count > 0)
            {
                result = true;
            }
            return result;

        }

        #region 修改
        [SupportFilter]
        public ActionResult Edit(string id)
        {
            PUB_TRANSPORTModel entity = m_BLL.GetById(id);
            return View(entity);
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Edit(PUB_TRANSPORTModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                if (isRepeated(model))
                {
                    return Json(JsonHandler.CreateMessage(0, "已存在名称为[" + model.TRANSPORT_NAME + "]的运输商，保存失败!"));
                }

                if (m_BLL.Edit(ref errors, model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "TRANSPORT_ID" + model.TRANSPORT_ID + ",TRANSPORT_NAME" + model.TRANSPORT_NAME, "成功", "修改", "PUB_TRANSPORT");
                    return Json(JsonHandler.CreateMessage(1, Resource.EditSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "TRANSPORT_ID" + model.TRANSPORT_ID + ",TRANSPORT_NAME" + model.TRANSPORT_NAME + "," + ErrorCol, "失败", "修改", "PUB_TRANSPORT");
                    return Json(JsonHandler.CreateMessage(0, Resource.EditFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.EditFail));
            }
        }
        #endregion

        #region 详细
        [SupportFilter]
        public ActionResult Details(string id)
        {
            PUB_TRANSPORTModel entity = m_BLL.GetById(id);
            return View(entity);
        }

        #endregion

        #region 删除
        [HttpPost]
        [SupportFilter]
        public ActionResult Delete(string id)
        {
            if(!string.IsNullOrWhiteSpace(id))
            {
                if (m_BLL.Delete(ref errors, id))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "TRANSPORT_ID:" + id, "成功", "删除", "PUB_TRANSPORT");
                    return Json(JsonHandler.CreateMessage(1, Resource.DeleteSucceed));
                }
                else
                {
                    string ErrorCol ="【该运输商已被使用】";
                    LogHandler.WriteServiceLog(GetUserId(), "TRANSPORT_ID" + id + "," + ErrorCol, "失败", "删除", "PUB_TRANSPORT");
                    return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail+ ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail));
            }
        }
        #endregion
        #region 导出导入
        [HttpPost]
        [SupportFilter]
        public ActionResult Import(string filePath)
        {
            var list = new List<PUB_TRANSPORTModel>();
            bool checkResult = m_BLL.CheckImportData(Utils.GetMapPath(filePath), list, ref errors);
            //校验通过直接保存
            if (checkResult)
            {
                m_BLL.SaveImportData(list);
                LogHandler.WriteServiceLog(GetUserId(), "导入成功", "成功", "导入", "PUB_TRANSPORT");
                return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
            }
            else
            {
                string ErrorCol = errors.Error;
                LogHandler.WriteServiceLog(GetUserId(), ErrorCol, "失败", "导入", "PUB_TRANSPORT");
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
            }
        }
        [HttpPost]
        [SupportFilter(ActionName = "Export")]
        public JsonResult CheckExportData()
        {
            List<PUB_TRANSPORTModel> list = m_BLL.GetList(ref setNoPagerAscById, "");
            if (list.Count().Equals(0))
            {
                return Json(JsonHandler.CreateMessage(0, "没有可以导出的数据"));
            }
            else
            {
                return Json(JsonHandler.CreateMessage(1, "可以导出"));
            }
        }
        [SupportFilter]
        public ActionResult Export()
        {
            List<PUB_TRANSPORTModel> list = m_BLL.GetList(ref setNoPagerAscById, "");
            JArray jObjects = new JArray();
                foreach (var item in list)
                {
                    var jo = new JObject();
                    jo.Add("TRANSPORT_ID", item.TRANSPORT_ID);
                    jo.Add("TRANSPORT_NAME", item.TRANSPORT_NAME);
                    jo.Add("KKSCODE", item.KKSCODE);
                    jo.Add("REMARKS", item.REMARKS);
                    jObjects.Add(jo);
                }
                var dt = JsonConvert.DeserializeObject<DataTable>(jObjects.ToString());
                var exportFileName = string.Concat(
                    "File",
                    DateTime.Now.ToString("yyyyMMddHHmmss"),
                    ".xlsx");
                return new ExportExcelResult
                {
                    SheetName = "Sheet1",
                    FileName = exportFileName,
                    ExportData = dt
                };
            }
        #endregion
    }
}

